//
//  EpointMutiPickerViewController.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-7-16.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointMutiPickerViewController.h"

@interface EpointMutiPickerViewController ()
{
    UITableView *tv;
    NSArray *strArr;
    NSMutableArray *selectedFlags;
}
@end

@implementation EpointMutiPickerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
//    NSMutableArray *arr = [[NSMutableArray alloc]init];
//    NSNumber *num = [NSNumber numberWithInt:1];
//    [arr addObject:num];
//    NSNumber *a = [arr objectAtIndex:0];
//    NSLog(@"num:%i",a.intValue);
    strArr = [self.passParam objectForKey:@"strArr"];
    
//    strArr = [NSArray arrayWithObjects:@"中国",@"韩国",@"朝鲜",@"美国", nil];
    selectedFlags = [[NSMutableArray alloc]init];
    for (int i=0; i<strArr.count; i++) {
        [selectedFlags addObject:@"0"];
    }
    tv = [[UITableView alloc]initWithFrame:[self getFullContentFrame] style:UITableViewStyleGrouped];
    tv.dataSource = self;
    tv.delegate = self;
    tv.backgroundView = nil;
    tv.backgroundColor = [UIColor clearColor];
    [self.view setBackgroundColor:COLOR(239, 239, 243)];
    [self.view addSubview:tv];
    [self setRightBarButtonItemWithTitle:@"确定" action:@selector(confirmAction)];
}

-(void)confirmAction
{
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    for (int i=0; i<selectedFlags.count; i++) {
        NSString *flag = [selectedFlags objectAtIndex:i];
        if ([flag isEqualToString:@"1"])
        {
            NSNumber *num = [NSNumber numberWithInt:i];
            [arr addObject:num];
        }
    }
    self.handler(arr);
    [self popBack];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return strArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"simplecell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]init];
    }
    cell.textLabel.text = [strArr objectAtIndex:indexPath.row];
    NSString *flag = [selectedFlags objectAtIndex:indexPath.row];
    if ([flag isEqualToString:@"1"]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *flag = [selectedFlags objectAtIndex:indexPath.row];
    if ([flag isEqualToString:@"0"]) {
        [selectedFlags setObject:@"1" atIndexedSubscript:indexPath.row];
    }
    else
    {
        [selectedFlags setObject:@"0" atIndexedSubscript:indexPath.row];
    }
    [tv reloadData];
}

+(void)InvokeMutiPickerViewController:(UIViewController *)vc title:(NSString *) vctitle strArr:(NSArray *)strArr handler:(MutiSelectHandler)handler
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:vctitle forKey:@"title"];
    [params setObject:strArr forKey:@"strArr"];
    EpointMutiPickerViewController *espv = [[EpointMutiPickerViewController alloc]init];
    espv.handler = handler;
    espv.passParam = params;
    [vc.navigationController pushViewController:espv animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
